package com.happy.zebra.admin.ui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.happy.zebra.admin.ui.entity.SmMemberActivate;
import com.happy.zebra.admin.ui.service.SmMemberActivateService;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import java.util.List;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/opt")
public class OptController {

	/** 硬件激活权限 service */
	private final SmMemberActivateService activateService;

	@DeleteMapping("/udid/clearRepeatData/{id}")
	public R clearRepeatData(@PathVariable String id) {

		if (id == null) {
			return new R(CommonConstants.FAIL, "UDID尚未激活，无法清理", false);
		}

		SmMemberActivate memberActivate = activateService.getById(id);

		if (StringUtils.isNotBlank(memberActivate.getMemberId())) {
			return new R(CommonConstants.FAIL, "UDID已被用户使用，暂时无法清理", false);
		}

		//UDID 仅此一个，无法清理
		List<SmMemberActivate> udids = activateService.list(new QueryWrapper<SmMemberActivate>().eq("udid", memberActivate.getUdid()));

		if (udids.size() < 2) {
			return new R(CommonConstants.FAIL, "UDID小于两条，无法清理", false);
		}

		//清理
		return new R(activateService.removeById(id));
	}

}
